/**
 * @author Wang Hai Long  [whailong2010@gmail.com]
 * @date Sep 27, 2014 6:19:04 PM
 */
package com.surent.controller.activity;

import me.imid.swipebacklayout.lib.SwipeBackLayout;
import me.imid.swipebacklayout.lib.app.SwipeBackActivity;
import android.app.ActionBar;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.avos.avoscloud.AVException;
import com.avos.avoscloud.LogInCallback;
import com.hailong.surent.R;
import com.lidroid.xutils.ViewUtils;
import com.lidroid.xutils.view.annotation.ContentView;
import com.lidroid.xutils.view.annotation.ViewInject;
import com.lidroid.xutils.view.annotation.event.OnClick;
import com.surent.model.dbmodel.User;
import com.surent.view.custom.toast.AppToast;

/**
 * @author Wang Hai Long [whailong2010@gmail.com]
 * @date Sep 27, 2014 6:19:04 PM
 */
@ContentView(R.layout.aty_login)
public class AtyLogin extends SwipeBackActivity {

	@ViewInject(R.id.et_atylogin_phoneNumber)
	private EditText etPhoneNum;

	@ViewInject(R.id.et_atylogin_password)
	private EditText etPassword;

	@ViewInject(R.id.btn_atylogin_login)
	private Button btnLogin;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		ViewUtils.inject(this);
		getSwipeBackLayout().setEdgeTrackingEnabled(SwipeBackLayout.EDGE_LEFT);
		ActionBar ab = getActionBar();
		ab.setDisplayHomeAsUpEnabled(true);
	}

	@OnClick(R.id.btn_atylogin_login)
	public void loginOnClick(View view) {
		User.logInInBackground(etPhoneNum.getText().toString(), etPassword.getText().toString(),
				new LogInCallback<User>() {

					@Override
					public void done(User user, AVException e) {
						if (e != null) {
							AppToast.makeText(AtyLogin.this, "用户名或密码错误", AppToast.ALERT);
						} else {
							AtyLogin.this.finish();
						}
					}
				}, User.class);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.aty_login, menu);
		return super.onCreateOptionsMenu(menu);
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		int id = item.getItemId();
		if (id == R.id.menu_atyLogin_signup) {
			startActivity(new Intent(this, AtyRegister.class));
		}
		if (id == android.R.id.home) {
			this.finish();
		}
		return super.onOptionsItemSelected(item);
	}
}
